<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('member.contract.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item text-center">
              <el-row :gutter="20">
                <el-col :span="4">
                  <el-form-item :label="i18n('member.avatar')" prop="avatar">
                    <img v-if="dataForm.avatar" :src="dataForm.avatar" class="avatar-upload"/>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item :label="i18n('member.username')" prop="username">
                    {{ dataForm.username }}
                  </el-form-item>

                  <el-form-item :label="i18n('member.chinese_nickname')" prop="chinese_nickname">
                    {{ dataForm.chinese_nickname }}
                  </el-form-item>

                  <el-form-item :label="i18n('member.chinese_mobile')" prop="chinese_mobile">
                    {{ dataForm.chinese_mobile }}
                  </el-form-item>

                  <el-form-item :label="i18n('contract.chinese_title')" v-if="dataForm.role_id == 2">
                    <el-text tag="b" type="primary">
                      {{ dataForm.contract.title }}
                    </el-text>
                  </el-form-item>

                  <el-form-item :label="i18n('member.contract.valid_time')" v-if="dataForm.role_id == 2">
                    <el-text tag="b" type="primary">
                      {{ dataForm.contract.valid_time }}
                    </el-text>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('member.contract.info') }}</span>
              </div>
            </template>
            <div class="text item text-center">
              <el-form-item :label="i18n('contract.chinese_title')" prop="contract_id">
                <el-select v-model="dataForm.contract_id" :placeholder="i18n('common.please_select') + i18n('contract.chinese_title')">
                  <el-option v-for="(v, k) in contractList" :label="v.chinese_title" :key="k" :value="v.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:member:contract:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="member-contract-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('member')

  const contractList = ref([])

  const dataForm = reactive({
    id: 0,
    role_id: 1,
    contract_id: 1,
    avatar: '',
    username: '',
    chinese_nickname: '',
    chinese_mobile: '',
    contract: {}
  })

  const dataRule = reactive({})

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handleAvatarSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.member_id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/member/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.data && data.status === 200) {
            dataForm.role_id = data.data.role_id
            dataForm.avatar = data.data.avatar
            dataForm.username = data.data.username
            dataForm.chinese_nickname = data.data.chinese_nickname
            dataForm.chinese_mobile = data.data.chinese_mobile
          }
        }).then(() => {
          if (dataForm.id && dataForm.role_id == 2) {
            $http({
              url: $http.adornUrl(`/${model.value}/contract/data`),
              method: 'get',
              params: $http.adornParams({
                'member_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.data && data.status === 200) {
                dataForm.contract.valid_time = data.data.format_valid_time
                if(data.data.contract){
                  dataForm.contract.title = data.data.contract.chinese_title
                }
              }
            })
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/contract/handle`),
          method: 'post',
          data: $http.adornData({
            member_id: dataForm.id,
            contract_id: dataForm.contract_id,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const loadContractList = () => {
    $http({
      url: $http.adornUrl('/contract/select'),
      method: 'get',
      params: $http.adornParams({
        category_id: 1,
        is_hidden: 2
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        contractList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)

    loadContractList()
  })
</script>
